import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserManagementActivity extends Activity {

    private EditText usernameEditText;
    private EditText passwordEditText;
    private Button addUserButton;
    private Button editUserButton;
    private Button deleteUserButton;
    private ListView userListView;

    private List<Map<String, Object>> userDataList;
    private int currentSelectedIndex = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_management);

        usernameEditText = findViewById(R.id.username_edittext);
        passwordEditText = findViewById(R.id.password_edittext);
        addUserButton = findViewById(R.id.add_user_button);
        editUserButton = findViewById(R.id.edit_user_button);
        deleteUserButton = findViewById(R.id.delete_user_button);
        userListView = findViewById(R.id.user_listview);

        userDataList = new ArrayList<>();

        addUserButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = usernameEditText.getText().toString();
                String password = passwordEditText.getText().toString();

                Map<String, Object> userMap = new HashMap<>();
                userMap.put("username", username);
                userMap.put("password", password);

                userDataList.add(userMap);

                refreshUserListView();

                usernameEditText.setText("");
                passwordEditText.setText("");
            }
        });

        editUserButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentSelectedIndex!= -1) {
                    Map<String, Object> selectedUser = userDataList.get(currentSelectedIndex);

                    selectedUser.put("username", usernameEditText.getText().toString());
                    selectedUser.put("password", passwordEditText.getText().toString());

                    refreshUserListView();
                }
            }
        });

        deleteUserButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentSelectedIndex!= -1) {
                    userDataList.remove(currentSelectedIndex);
                    currentSelectedIndex = -1;

                    refreshUserListView();
                }
            }
        });

        userListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentSelectedIndex = position;

                Map<String, Object> selectedUser = userDataList.get(position);

                usernameEditText.setText(selectedUser.get("username").toString());
                passwordEditText.setText(selectedUser.get("password").toString());
            }
        });
    }

    private void refreshUserListView() {
        SimpleAdapter adapter = new SimpleAdapter(this, userDataList, R.layout.user_item,
                new String[]{"username", "password"},
                new int[]{R.id.username_item, R.id.password_item});

        userListView.setAdapter(adapter);
    }
}